package com.example.module_device

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.RequestEvent
import com.example.lib_common.base.state.Success
import com.example.module_device.model.DeviceEntityItem

import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class DeviceViewModel @Inject constructor(private val repo : DeviceRepository) : BaseViewModel() {
    private val mCarState_ = MutableStateFlow<RequestEvent>(Loading)
    val mCarState = mCarState_.asStateFlow()
     val mDeviceList = MutableLiveData<MutableList<DeviceEntityItem>>()


    private val mAddDeviceState_ = MutableStateFlow<RequestEvent>(Loading)
    val mAddDeviceState = mAddDeviceState_.asStateFlow()

    fun getDeviceAll(){
        execute(
            action = repo.getDeviceAll(),
            onError = {
                mCarState_.value = Failed("数据获取失败"+it)
            },
            onFinish = {
                mCarState_.value = Success
                mDeviceList.value = it.data
            }
        )
    }
    fun getAddDevice(param : DeviceEntityItem){
        execute(
            action = repo.getAddDevice(param),
            onError = {
                mAddDeviceState_.value = Failed("数据获取失败"+it)
            },
            onFinish = {
                mAddDeviceState_.value = Success
            }
        )
    }

}